package com.suzus.aiService.common.utils;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStreamReader;

@Component
public class SearchWords {
    public String search(String wenzi){
        // CSV文件路径
        String csvFile = "db/药品清单下发版.csv";
        ClassPathResource resource = new ClassPathResource(csvFile);
        // 要查询的字段和值
        String queryValue = wenzi;
        String res = "";
        try (CSVReader reader = new CSVReader(new InputStreamReader(resource.getInputStream()))) {
            String[] headers = reader.readNext(); // 读取CSV文件的标题行
            String[] line;
            while ((line = reader.readNext()) != null) {
                String genericName = line[0]; // 获取当前行的通用名称
                if (genericName.contains(queryValue)) {
                    // 如果通用名称匹配目标值，则打印当前行信息
                    res = line[0];
                    break; // 如果只要找到一行匹配记录，可以使用break跳出循环
                }
            }
        } catch (IOException | CsvValidationException e) {
            e.printStackTrace();
        }
        return res;
    }
}
